package com.macro.mall.product.controller;

import com.google.api.client.util.Maps;
import com.macro.mall.common.api.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

import java.util.Map;

/**
 * 库存管理管理Controller
 */
@Slf4j
@Controller
@Api(tags = "PmProdStockController", description = "出入库管理")
@RequestMapping("/prodStock")
public class PmProdStockController {

    @Autowired
    private RestTemplate restTemplate;

    @Value("${host.mall.product}")
    private String HOST_MALL_PRODUCT;

    private String SERVICE_NAME="/prodStock";

    @ApiOperation(value = "增加库存")
    @RequestMapping(value = "/addStock", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<String> addStock(@RequestParam(value = "changeStock", defaultValue = "") Float changeStock,
                                                    @RequestParam(value = "stockId", defaultValue = "") Long stockId
    ) {
        String url = HOST_MALL_PRODUCT +SERVICE_NAME+ "/addStock";

        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
        Map<String,Object> param = Maps.newHashMap();
        param.put("changeStock",changeStock);
        param.put("stockId",stockId);

        param.entrySet().stream().forEach(o -> builder.queryParam(o.getKey(),o.getValue()));
        String restUrl = builder.build().encode().toString();
        log.debug("★★★★★★PmProdStockController.addStock   restUrl="+restUrl);

        ResponseEntity<CommonResult> responseEntity = restTemplate.getForEntity(restUrl, CommonResult.class);
        return responseEntity.getBody();
    }
    @ApiOperation(value = "扣减库存")
    @RequestMapping(value = "/deductStock", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<String> deductStock(@RequestParam(value = "changeStock", defaultValue = "") Float changeStock,
                                            @RequestParam(value = "stockId", defaultValue = "") Long stockId,
                                            @RequestParam(value = "orderId", defaultValue = "") String orderId
    ) {
        String url = HOST_MALL_PRODUCT +SERVICE_NAME+ "/deductStock";

        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
        Map<String,Object> param = Maps.newHashMap();
        param.put("changeStock",changeStock);
        param.put("stockId",stockId);
        param.put("orderId",orderId);

        param.entrySet().stream().forEach(o -> builder.queryParam(o.getKey(),o.getValue()));
        String restUrl = builder.build().encode().toString();
        log.debug("★★★★★★PmProdStockController.deductStock   restUrl="+restUrl);

        ResponseEntity<CommonResult> responseEntity = restTemplate.getForEntity(restUrl, CommonResult.class);
        return responseEntity.getBody();
    }
    @ApiOperation(value = "回退库存")
    @RequestMapping(value = "/rollbackStock", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<String> rollbackStock(@RequestParam(value = "changeStock") Long prodId,
                                              @RequestParam(value = "stockId") Long skuInstId,
                                              @RequestParam(value = "siteId") Integer siteId,
                                              @RequestParam(value = "stockDate") String stockDate,
                                              @RequestParam(value = "stockTime") String stockTime,
                                              @RequestParam(value = "changeStock") Float changeStock,
                                              @RequestParam(value = "orderId") String orderId
    ) {
        String url = HOST_MALL_PRODUCT +SERVICE_NAME+ "/rollbackStock";

        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
        Map<String,Object> param = Maps.newHashMap();
        param.put("prodId",prodId);
        param.put("skuInstId",skuInstId);
        param.put("siteId",siteId);
        param.put("stockDate",stockDate);
        param.put("stockTime",stockTime);
        param.put("changeStock",changeStock);
        param.put("orderId",orderId);

        param.entrySet().stream().forEach(o -> builder.queryParam(o.getKey(),o.getValue()));
        String restUrl = builder.build().encode().toString();
        log.debug("★★★★★★PmProdStockController.rollbackStock   restUrl="+restUrl);

        ResponseEntity<CommonResult> responseEntity = restTemplate.getForEntity(restUrl, CommonResult.class);
        return responseEntity.getBody();
    }

    @ApiOperation(value = "回退库存--简化版")
    @RequestMapping(value = "/rollbackStockSimplify", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<String> rollbackStockSimplify(@RequestParam(value = "orderId") String orderId
    ) {
        String url = HOST_MALL_PRODUCT +SERVICE_NAME+ "/rollbackStockSimplify";

        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
        Map<String,Object> param = Maps.newHashMap();
        param.put("orderId",orderId);

        param.entrySet().stream().forEach(o -> builder.queryParam(o.getKey(),o.getValue()));
        String restUrl = builder.build().encode().toString();
        log.debug("★★★★★★PmProdStockController.rollbackStockSimplify   restUrl="+restUrl);

        ResponseEntity<CommonResult> responseEntity = restTemplate.getForEntity(restUrl, CommonResult.class);
        return responseEntity.getBody();
    }
}
